<template>
  <div class="login-container">
    <div class="title1">
      <div style="font-size: 40px"><h1>EasyCTBA</h1></div>
      <div style="font-size: 23px">基于微服务的课堂教学行为分析平台</div>
    </div>

    <el-card>
      <el-form ref="registerForm" :model="registerForm" :rules="rules" class="login-form" label-width="80px">
        <h1 class="title">用户注册</h1>
        <el-form-item label="账号" prop="username">
          <el-input v-model="registerForm.username" type="text" placeholder="请输入账号" prefix-icon="el-icon-user-solid"></el-input>
        </el-form-item>
        <el-form-item label="密码" prop="password">
          <el-input v-model="registerForm.password" type="password" placeholder="请输入密码" prefix-icon="iconfont icon-mima"></el-input>
        </el-form-item>
        <el-form-item label="密码" prop="confirmPassword">
          <el-input v-model="registerForm.confirmPassword" type="password" placeholder="确认密码" prefix-icon="iconfont icon-mima"></el-input>
        </el-form-item>
        <el-form-item label="验证码" prop="code">
          <div style="display: flex; justify-content:space-between;">
            <el-input v-model="registerForm.code" size="large" placeholder="验证码" style="width: 63%" prefix-icon="iconfont icon-yanzhengyanzhengma"></el-input>
            <img :src="codeUrl" @click="getCode" alt="验证码" style="height: 40px; width: 35%"/>
          </div>

        </el-form-item>
        <el-form-item>
          <el-button type="primary" style="margin-left: 100px" @click="register">注册</el-button>
        </el-form-item>
        <el-link @click="login" :underline="false" style="float: right;margin-bottom: 10px">使用已有账号登录</el-link>
      </el-form>
    </el-card>

  </div>
</template>

<script>
import {getCodeImg, register} from "@/api/login";
export default {
  name: "RegisterIndex",
  data(){
    return{
      registerForm: {
        username: '',
        password: '',
        confirmPassword:'',
        code:'',
        uuid:'',
        userType: "caba_user"
      },
      codeUrl:'',
      captchaEnabled: true,
      token:''
    }
  },
  computed:{
    //校验
    rules() {
      const equalToPassword = (rule, value, callback) => {
        if (this.registerForm.password !== value) {
          callback(new Error("两次输入的密码不一致"));
        } else {
          callback();
        }
      };
      return{
        username: [
          { required: true, trigger: "blur", message: "请输入您的账号" },
          { min: 2, max: 20, message: "用户账号长度必须介于 2 和 20 之间", trigger: "blur" }
        ],
        password: [
          { required: true, trigger: "blur", message: "请输入您的密码" },
          { min: 5, max: 20, message: "用户密码长度必须介于 5 和 20 之间", trigger: "blur" }
        ],
        confirmPassword: [
          { required: true, trigger: "blur", message: "请再次输入您的密码" },
          { required: true, validator: equalToPassword, trigger: "blur" }
        ],
        code: [{ required: true, trigger: "change", message: "请输入验证码" }]
      }
    },
  },
  methods:{
    getCode() {
      getCodeImg().then( data =>{
        this.captchaEnabled=data.data.captchaEnabled
        if (this.captchaEnabled) {
          this.codeUrl = 'data:image/gif;base64,' + data.data.img;
          this.registerForm.uuid = data.data.uuid;
        }
      })
    },
    login(){
      this.$router.push('/login')
    },
    register(){
      this.$refs.registerForm.validate(valid => {
        if (valid) {
          register({
            username: this.registerForm.username,
            password: this.registerForm.password,
            confirmPassword: this.registerForm.confirmPassword,
            code: this.registerForm.code,
            uuid:this.registerForm.uuid,
            userType:this.registerForm.uuid
          }).then(data => {
            console.log(data)
          })
        }
      });
    }
  },
  mounted() {
    this.getCode();
  }
}
</script>

<style scoped>
.title1{
  justify-content: center;
  align-items: center;
  color: white;
  display: flex;
  flex-direction: column;
  margin-right: 400px;
}
.login-container {
  display: flex;
  justify-content: center;
  align-items: center;
  margin-left: 200px;
}

.login-form {
  width: 400px;
}

.title {
  font-size: 24px;
  margin-bottom: 20px;
  text-align: center;
}
</style>